package com.boot.security.server.controller;

import java.util.Date;
import java.util.List;

import com.boot.security.server.dao.DictDao;
import com.boot.security.server.model.Dict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.dao.MQuestionDao;
import com.boot.security.server.model.Question;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/mQuestions")
public class MQuestionController {

    @Autowired
    private MQuestionDao mQuestionDao;
    @Autowired
    private DictDao dictDao;

    @PostMapping
    @ApiOperation(value = "保存")
    public Question save(@RequestBody Question mQuestion) {
        mQuestionDao.save(mQuestion);
        // 字典添加字段
        Dict d = dictDao.getByTypeAndK("questionType", mQuestion.getId().toString());
        if (d != null) {
            throw new IllegalArgumentException("类型和key已存在");
        }
        Dict dict = new Dict();
        dict.setK(mQuestion.getId().toString());
        dict.setType("questionType");
        dict.setVal(mQuestion.getQuestion());
        dict.setCreateTime(new Date());
        dictDao.save(dict);
        return mQuestion;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public Question get(@PathVariable Long id) {
        return mQuestionDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public Question update(@RequestBody Question mQuestion) {
        mQuestionDao.update(mQuestion);

        return mQuestion;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return mQuestionDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<Question> list(PageTableRequest request) {
                return mQuestionDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @PostMapping("/getList")
    @ApiOperation(value = "列表")
    public List<Question> getList() {
        return mQuestionDao.getList();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        mQuestionDao.delete(id);
    }
}
